semaphore mutex = 1, linux = 0, ms = 0;
int numHackers = 0, numMicrosoft = 0;

hacker()
{
    p(mutex);
    numHackers++;
    if( numHackers == 4)
    {
        v(linux); v(linux); v(linux);
        numHackers -= 4;
        v(mutex);
        getOnBoard ();
    }
    else if(numHackers >= 2 && numSerfs >= 2)
    {
        v(linux); v(ms); v(ms);
        numHackers -= 2;
        numSerfs -= 2;
        v(mutex);
        getOnBoard ();
    }
    else
    {
        v(mutex);
        p(linux);
        getOnBoard ();
    }
}

Microsoft()
{
    p(mutex);
    numMicrosoft++;
    if( numMicrosoft == 4)
    {
        v(ms); v(ms); v(ms);
        numMicrosoft -= 4;
        v(mutex);
        getOnBoard ();
    }
    else if(numMicrosoft >= 2 && numHackers >= 2)
    {
        v(ms); v(linux); v(linux);
        numHackers -= 2;
        numMicrosoft -= 2;
        v(mutex);
        getOnBoard ();
    }
    else
    {
        v(mutex);
        p(ms);
        getOnBoard ();
    }
}

Semaphore boardMutex = 1;
int numBoarded = 0;
getOnBoard()
{
    p(boardMutex);
    numBoarded++;
    board();
    if (numBoarded == 4)
    {
        rowboat();
        numBoarded = 0;
    }
    v(boardMutex);
}

